/*
 * xdata.h
 *
 * data flash and sleep ram driver.
 * It is not recommended to use data flash, and its writing requires DCIN to have 5V
 * sleep ram, Power failure does not save
 * 使用XData方式的读写实现sleep模式下对XDATA数据的保存。通过工程配置xdata最后16字节不使用
 * 项目配置默认开启
 * 
 * Copyright (C) 2022 Microwell, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: guoming<guoming@microwell.net>
 */
#ifndef _XDATA__H_
#define _XDATA__H_

#include "core.h"

#if (CONFIG_IC_TYPE == CHIP_SS888X)

enum SR_IDX {
    SR_IDX_H1,
    // SR_IDX_H2,
    SR_IDX_D1,
    SR_IDX_D2,
    SR_IDX_CRC,
    SR_VALID_SIZE
};


#define CONFIG_DATA_FLASH                          (RESET)

#define DATA_FLAHS_ENABLE()                        CLEAR_EFR_BIT(UDBCON, (UDBSEL | DF_RW_NO))
#define SLEEP_RAM_ENABLE()                         MODIFY_EFR_REG(UDBCON, (UDBSEL | DF_RW_NO), UDBSEL)

void xdata_init(void);

void data_flash_write(uint8_t addr, uint8_t dat);
uint8_t data_flash_read(uint8_t addr);
void data_flash_buf_write(uint8_t addr, uint8_t num, uint8_t *dat);
void data_flash_buf_read(uint8_t addr, uint8_t num, uint8_t *dat);

bool slpram_crc_check(void);
uint8_t slpram_rd_dat(uint8_t enum_num);
void slpram_wr_dat(uint8_t enum_num, uint8_t dat);
#endif

#endif